import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ValidateProvider} from "../../providers/validate/validate";
import {StorageProvider} from "../../providers/storage/storage";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ConfigProvider} from "../../providers/config/config";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/wallet',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-money',
    templateUrl: 'money.html',
})
export class MoneyPage {
    token: any;
    currentTab = 'payment';
    tabInfo = {
        payment: {
            name: 'peyment',
            default: 'assets/imgs/m-zf.png',
            active: 'assets/imgs/m-zf-active.png'
        },
        income: {
            name: 'income',
            default: 'assets/imgs/m-sr.png',
            active: 'assets/imgs/m-sr-active.png'
        },
        warehouse: {
            name: 'warehouse',
            default: 'assets/imgs/m-kf.png',
            active: 'assets/imgs/m-kf-active.png'
        }
    };
    pageInfo = {
        payment: {
            page: 2
        },
        income: {
            page: 2
        },
        warehouse: {
            page: 2
        }
    };

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public storage: StorageProvider,
        public httpService: HttpServicesProvider,
        public config: ConfigProvider,
        public util: UtilsProvider
    ) {
        // if (!this.validate.checkLogin()) {
        //     this.navCtrl.push('LoginPage');
        // }
    }

    //判断用户是否登录
    ionViewCanEnter():boolean{
        return this.validate.checkLogin();
    }

    ionViewWillEnter() {
        this.token = this.config.getToken();
        this.getWalletInfo();
    }

    walletInfo: any;
    detailInfo: any;

    getWalletInfo() {
        this.httpService.requestPostData('/user/wallet', {
            token: this.config.getToken()
        }, response => {
            let data = response['data'];
            this.walletInfo = {
                payment: data['expen'],//支出
                user_money: data['user_money'],//余额
                income: data['income']//收入
            };
            this.detailInfo = {
                payment: data['expen_list']['data'],
                income: data['income_list']['data'],
                warehouse: []
            };
        });
    }

    changeTab(tabName) {
        this.currentTab = tabName;
        this.pageInfo = {
            payment: {
                page: 2
            },
            income: {
                page: 2
            },
            warehouse: {
                page: 2
            }
        };
    }

    getMoreInfo(infiniteScroll) {
        let apiUrl = "";
        switch (this.currentTab) {
            case 'payment':
                apiUrl = "/user/expen_list";
                this.httpService.requestData(apiUrl + '/token/' + this.token + '/page/' + this.pageInfo.payment.page, response => {
                    let data = response['data'];
                    this.detailInfo.payment = this.detailInfo.payment.concat(data['data']);
                    this.pageInfo.payment.page++;
                    if (infiniteScroll) {
                        infiniteScroll.complete();
                        /*请求完成数据更新页面*/
                        if (this.pageInfo.payment.page < data['last_page']) {   /*没有数据 禁用上拉更新*/
                            infiniteScroll.enable(false);
                        }else{
                            this.detailInfo.payment = this.detailInfo.payment.concat(data['data']);
                        }
                    }
                });
                break;
            case 'income':
                apiUrl = "/user/income_list";
                this.httpService.requestData(apiUrl + '/token/' + this.token + '/page/' + this.pageInfo.income.page, response => {
                    let data = response['data'];
                    this.detailInfo.payment = this.detailInfo.payment.concat(data['data']);
                    this.pageInfo.payment.page++;                    
                    if (infiniteScroll) {
                        infiniteScroll.complete();
                        /*请求完成数据更新页面*/
                        if (!this.pageInfo.payment.page<data['last_page']) {  /*没有数据 禁用上拉更新*/                            
                            infiniteScroll.enable(false);
                        }else{
                            this.detailInfo.payment = this.detailInfo.payment.concat(data['data']);
                        }
                    }
                });
                break;
            case 'warehouse':
                break;
            default:
                return;
        }

    }

    doInfinite(infiniteScroll) {
        this.getMoreInfo(infiniteScroll);
    }

    //解决安卓平台下不能调起微信支付的问题
    goRecharge(){
        let apiUrl = "/user_info/check_level";
        this.httpService.requestPostData(apiUrl,{
            token: this.config.getToken()
        }, (response)=>{
            if(response['error'] == 200 && response['reg_step'] == 2){
                if(location.href.indexOf('?') > -1){
                    this.navCtrl.push('MoneyRechargePage');
                }else{
                    location.href = location.href.split("#")[0].split('?')[0]+"?#/member/wallet/recharge";
                }
            }else{
                this.util.presentConfirm('您无权使用此功能，请完善注册资料；现在去完善？', ()=>{
                    this.navCtrl.push('RegisterPage');
                });
            }
        });


    }

    goWithdraw(){
        let apiUrl = "/user_info/check_level";
        this.httpService.requestPostData(apiUrl,{
            token: this.config.getToken()
        }, (response)=>{
            if(response['error'] == 200 && response['reg_step'] == 2){
                this.navCtrl.push('MoneyTsPage');
            }else{
                this.util.presentConfirm('您无权使用此功能，请完善注册资料；现在去完善？', ()=>{
                    this.navCtrl.push('RegisterPage');
                });
            }
        });
    }
}
